package com.yanqu.road.dao.impl.channelcenter.channelmusicroom;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.channelmusicroomrank.ChannelMusicRoomRankDetailConfig;


import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ChannelMusicRoomRankActivityRankConfigDaoImpl extends TempNoPoolCacheDao {

    public static String getMusicRoomRankConfigSql = "select * from `t_n_activity_musicroom_rank` where `activityId`=?";

    public ChannelMusicRoomRankActivityRankConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    public List<ChannelMusicRoomRankDetailConfig> getRankConfigList(int activityId) {
        SqlCacheParam sqlCacheParam = getCacheParam(getMusicRoomRankConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (List<ChannelMusicRoomRankDetailConfig>) cacheResult;
        }
        List<ChannelMusicRoomRankDetailConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelMusicRoomRankDetailConfig config = parseRankConfig(rs);
                list.add(config);
            }
        }catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }finally {
            closeConnection(call, rs);
        }
        return list;
    }

    private ChannelMusicRoomRankDetailConfig parseRankConfig(ResultSet rs) throws SQLException {
        ChannelMusicRoomRankDetailConfig data = new ChannelMusicRoomRankDetailConfig();
        data.setActivityId(rs.getInt("activityId"));
        data.setId(rs.getInt("id"));
        data.setName(rs.getString("name"));
        data.setDesc(rs.getString("desc"));
        data.setRank(rs.getInt("rank"));
        data.setScore(rs.getInt("score"));
        data.setReward(rs.getString("rewards"));
        data.setIcon(rs.getString("icon"));
        return data;
    }
}
